package com.gaoyi.crm.control;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextWatcher;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatEditText;
import androidx.appcompat.widget.LinearLayoutCompat;
import androidx.databinding.BindingAdapter;
import androidx.databinding.InverseBindingAdapter;
import androidx.databinding.InverseBindingListener;
import androidx.lifecycle.MutableLiveData;

import com.gaoyi.crm.R;
import com.gaoyi.crm.callback.OnEndClickListener;
import com.gaoyi.crm.databinding.ViewEditInputBinding;
import com.gaoyi.crm.util.DensityUtil;

public class EditInputView extends BaseView<ViewEditInputBinding> {
    public EditInputView(@NonNull Context context) {
        super(context);
    }

    public EditInputView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public EditInputView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public EditInputView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void initView(TypedArray ta) {
        String title = ta.getString(R.styleable.EditInputView_edit_input_title);
        boolean star = ta.getBoolean(R.styleable.EditInputView_edit_input_star, false);
        boolean isSpinner = ta.getBoolean(R.styleable.EditInputView_edit_input_is_spinner, false);
        float defaultHeight = DensityUtil.dp2px(40);
        float height = ta.getDimension(R.styleable.EditInputView_edit_input_height, defaultHeight);
        if (star) {
            Spannable spannable = new SpannableString("*" + title);
            ForegroundColorSpan colorSpan = new ForegroundColorSpan(mContext.getResources().getColor(R.color.red));
            spannable.setSpan(colorSpan, 0, 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
            mBinding.tvInputTitle.setText(spannable);
        } else {
            mBinding.tvInputTitle.setText(title);
        }

        if (isSpinner) { // 包含下拉框
            mBinding.etInput.setEndDrawable(R.drawable.ic_spinner);
        }

        setEditTextHeight((int) height);

        // 高度小时，居中显示，高度高时，Title顶部显示
        int gravity = height == defaultHeight ? Gravity.CENTER_VERTICAL : Gravity.NO_GRAVITY;
        mBinding.llInput.setGravity(gravity);
        mBinding.etInput.setGravity(gravity);
    }

    public void setEditTextHeight(int value) {
        LinearLayoutCompat.LayoutParams lp = (LinearLayoutCompat.LayoutParams) mBinding.etInput.getLayoutParams();
        lp.height = value;
        mBinding.etInput.setLayoutParams(lp);
    }

    @Override
    protected ViewEditInputBinding getViewBinding() {
        return ViewEditInputBinding.inflate(LayoutInflater.from(mContext));
    }

    public AppCompatEditText getEditView() {
        return mBinding.etInput;
    }

    public void setText(String text) {
        mBinding.etInput.setText(text);
    }

    public String getText() {
        return mBinding.etInput.getText().toString();
    }

    public void setSelection(int index) {
        mBinding.etInput.setSelection(index);
    }

    public void setOnEndClickListener(OnEndClickListener onEndClickListener) {
        mBinding.etInput.setOnEndClickListener(onEndClickListener);
    }

    @Override
    protected int[] getAttrs() {
        return R.styleable.EditInputView;
    }

    public void addTextChangedListener(TextWatcher textWatcher) {
        mBinding.etInput.addTextChangedListener(textWatcher);
    }

    @BindingAdapter("content")
    public static void setInput(EditInputView v, MutableLiveData<String> text) {
        v.mBinding.etInput.setText(text.getValue());
    }

    @InverseBindingAdapter(attribute = "content")
    public static String getInput(EditInputView v) {
        return v.mBinding.etInput.getText().toString();
    }

    @BindingAdapter("contentAttrChanged")
    public static void setContentListener(EditInputView v, InverseBindingListener listener) {
        v.mBinding.etInput.setOnFocusChangeListener((view, b) -> {
            if (!b) {
                listener.onChange();
            }
        });
    }
}
